package com.hb.core.pojo.center.au.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.center.au.dto.UserDTO;
import com.hb.core.pojo.center.au.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * 系统用户表，表中用户包括两种类型，系统管理用户和招标业务用户 Mapper 接口
 *
 * @author zhaojk
 * @since 2021-11-29
 */
public interface UserMapper extends BaseMapper<User> {

    /**
     * 判断指定用户名或者ID的用户是否在黑名单
     *
     * @param usernameOrUserId
     * @return true: 被拉入黑名单, false: 未被拉入黑名单
     */
    @Select("select 1 from au_user_blacklist aub where aub.user_id=#{usernameOrUserId} or exists " +
            "(select 1 from au_user u where u.id=aub.user_id and u.username=#{usernameOrUserId})")
    Integer existsInBlackList(Serializable usernameOrUserId);

    /**
     * 查询指定角色的用户列表
     *
     * @param roleId
     * @return 列表
     */
    @Select("<script>select * from au_user u " +
            "where 1=1 <if test='orgId != null'>and u.org_id=#{orgId}</if> " +
            "and exists(select 1 from au_user_role ur where ur.role_id=#{roleId} and ur.user_id=u.id) " +
            "order by u.create_time desc</script>")
    List<User> listUsersByRoleId(@Param("roleId") Long roleId, @Param("orgId") Long orgId);

    /**
     * 查询指定角色的用户列表
     *
     * @param pages
     * @param roleId
     * @return 分页对象
     */
    @Select("<script>select * from au_user u " +
            "where 1=1 <if test='orgId != null'>and u.org_id=#{orgId}</if> " +
            "and exists(select 1 from au_user_role ur where ur.role_id=#{roleId} and ur.user_id=u.id) " +
            "order by u.create_time desc</script>")
    Page<User> pageUsersByRoleId(Page<User> pages, @Param("roleId") Long roleId, @Param("orgId") Long orgId);

    /**
     * @param pages:分页对象
     * @param username:用户名
     * @param orgName:企业名称
     * @param userStatus:用户状态
     * @param type:1          代理机构用户列表 2 投标人用户列表
     * @Description:分页查询用户列表
     * @Author: zhongdl
     * @Date:2022/3/23 13:51
     * @return: {@link Page< UserDTO> }
     **/
    Page<UserDTO> pageAuditUser(@Param("page") Page pages,
                                @Param("username") String username,
                                @Param("orgName") String orgName,
                                @Param("userStatus") Integer userStatus,
                                @Param("type") Integer type);

    /**
     * @param roleId
     * @param idNum
     * @return
     * @Description:根据角色ID和身份证号查询用户
     * @Author: ljs
     */
    User getUserByRoleAndIdNum(@Param("roleId") Long roleId, @Param("idNum") String idNum);

    /**
     * @param roleIds
     * @param phone
     * @return
     * @Description:根据角色ID和身份证号查询用户
     * @Author: ljs
     */
    List<User> getUserByPhoneAndRoleSign(@Param("roleIds") List<Long> roleIds, @Param("phone") String phone, @Param("orgId") Long orgId);

    User getUserByOrgIdAndRoleId(@Param("roleId") Long roleId, @Param("orgId") Long orgId);

    User getUserByUnifiedTransactionCodeAndRoleIds(@Param("roleIds") List<Long> roleIds, @Param("unifiedTransactionCode") String unifiedTransactionCode);

    User getUserByUnifiedTransactionCodeAndRoleSign(@Param("unifiedTransactionCode") String unifiedTransactionCode, @Param("roleSign") Integer roleSign);

    /**
     * 分页获取指定查询条件的用户列表
     *
     * @param pages
     * @param userDto
     */
    Page<User> pageUsers(Page<User> pages, @Param("userDTO") UserDTO userDTO);

    /**
     * 根据id查询投标人
     *
     * @param userList
     * @return org.fzzn.core.model.msg.Message
     * @author yuezheng
     * @date 2022/4/23 10:12 AM
     */
    List<User> finBidUserByUserList(@Param("userId") Set<Long> userId);


    /**
     * 查询当前企业信息首个用户的信息
     *
     * @author tian
     * @date 2022-5-6 17:58
     * @param orgId
     * @return com.hb.core.pojo.center.au.entity.User
     */
    User selectFirstUserByOrgId(Long orgId);

    /**
     * 查询企业下的管理员
     *
     * @author tian
     * @date 2022-5-19 13:56
     * @param userDTO
     * @return com.hb.core.pojo.center.au.entity.User
     */
    User findManagerByOrgId(UserDTO userDTO);
}
